﻿Public Class CCANCELLabelState : Inherits CStateBase

    ''' <summary>
    ''' 実行可能かを判断する
    ''' </summary>
    ''' <param name="task"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overrides Function IsEnter(ByVal task As CTaskBase) As Boolean
        Debug.WriteLine("CCANCELLabelState.IsEnter")
        If CCommon.isInStr(task.ActionCd, "CANCEL_LABEL") Then
            Return True
        Else
            Return False
        End If
    End Function

    ''' <summary>
    ''' 実行メソッド
    ''' </summary>
    ''' <param name="task"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overrides Function DoExecute(ByVal task As CTaskBase) As CStateBase
        Dim nextState As CStateBase
        Dim pdaTask As CPDATask = DirectCast(task, CPDATask)
        Debug.WriteLine("CCANCELLabelState.DoExecute")
        Dim checkInfo As CCheck = CCheck.GetIns
        Dim AppManager As CApplicationManager = CApplicationManager.GetInstance

        'Dim waitTask As CPersonal = New CPersonal(pdaTask)
        Debug.WriteLine("CCANCELLabelState.DoExecute...START")
        'MessageBox.Show("INNER_BARCD:" & task.ProcessText)

        If task.ProcessText = "" Then
            '' リーダーからデータを取得する(待ち時間制限なし)
            pdaTask.WindowManager.SetMessage(CConst.CNS_MSG_021, CConst.COLOR_BLACK, CConst.COLOR_GREEN)
            CCommon.FlagManage("CANCEL_STATUS", True)
            task.ProcessText = task.ReadTextWait(-1)
            Debug.WriteLine("読んだバーコード:" & task.ProcessText)
        End If

        Try

            ''バーコード判別
            Select Case CBarcode.GetBarcodeType(task.ProcessText)

                Case EnumBarcodeType.INNER_LABEL_CODE
                    'Unique.csvを検査
                    'なければ自分のユニークを検査->あれば自分のユニークDataで削除
                    'なければエラー
                    Dim uniqueCd As String = CBarcode.getBarcodeVl(task.ProcessText, EnumBarcodeType.INNER_LABEL_CODE, EnumBarcodeType.UNIQUE_CD)
                    Dim UniqueInfo As CCheck.UniqueCdData = checkInfo.ChkUniqueCdData(uniqueCd)

                    '過去履歴あるので取り消し可能-
                    'サーバーに送信判断
                    '   未送信の場合自分のもので送信済する
                    '   送信済の場合はサーバーに送信する
                    If Not UniqueInfo.uniqueCd Is Nothing Then
                        Dim historyOrderCd As String = checkInfo.GetLabelOrderCd(uniqueCd)
                        Dim labelArrayList As ArrayList = checkInfo.GetLabelData(historyOrderCd)

                        For i As Integer = 0 To labelArrayList.Count - 1
                            Dim labelData As CCheck.LabelData = labelArrayList(i)
                            If uniqueCd = labelData.uniqueCd Then
                                If labelData.isSend Then
                                    'サーバーに送信
                                    'MessageBox.Show("サーバーに送信")
                                Else
                                    'MessageBox.Show("LABEL_DATA_DELETE")
                                    'ラベル削除
                                    checkInfo.DelLabelData(task.ProcessText)
                                End If
                                '取消が終わったら
                                '残数プラス
                                'uniqueメモリにも削除
                                'ラベルにも削除
                                Dim inBoxQty As Integer = CCommon.Change36(CBarcode.getBarcodeVl(task.ProcessText, _
                                                                            CBarcode.GetBarcodeType(task.ProcessText), _
                                                                            EnumBarcodeType.INBOX_QTY))
                                'ユニークコード
                                'Dim orderCd As String = checkInfo.GetLabelOrderCd(uniqueCd)
                                Dim setData As COrderRecord = pdaTask.WindowManager.SetCurrentOrder(historyOrderCd)
                                setData.RemainedQty = CInt(setData.RemainedQty) + CInt(inBoxQty)

                                'uniqueメモリにも削除
                                If checkInfo.DelUniqueCdData(uniqueCd) Then
                                    'MessageBox.Show("UNIQUE_DATA_DELETE")
                                    Exit For
                                End If

                            End If
                        Next
                        CCommon.FlagManage("INNERLABEL", False)
                    Else
                        CPersonal.ErrorWindow(pdaTask, EnumWindowMode.ALERT_MODE, _
                                       Color.Black, CConst.EnumSound.ALERT_BUZZ, CConst.CNS_MSG_022, Color.White)
                    End If

                Case EnumBarcodeType.EMP_CODE
                    '交代
                    Debug.WriteLine("社員番号読みました。")

                    If Not AppManager.m_AppSetting.IsStaticReadEmpCd Then
                        '社員番号
                        '交代
                        'waitTask.Waiting(CCommon.CNS_MSG_064, pdaTask.WindowManager.GetWindowMode())
                        Dim EmpData As CCheck.EmpData = checkInfo.ChkEmpData(task.ProcessText)
                        pdaTask.WindowManager.SetEmpInfo(EmpData.empCd & ":[" & EmpData.empName & "]") '
                        pdaTask.EmpCd = EmpData.empCd
                    Else
                        CPersonal.ErrorWindow(pdaTask, EnumWindowMode.ALERT_MODE, _
                                            Color.Black, CConst.EnumSound.ALERT_BUZZ, CConst.CNS_MSG_020, Color.White)
                    End If
                    task.ProcessText = ""
                    task.ActionCd = "CANCEL_LABEL"
                    Return DoBaseExecute(task)
                Case Else
                    '社内ラベル以外はエラー
                    CPersonal.ErrorWindow(pdaTask, EnumWindowMode.ALERT_MODE, _
                                       Color.Black, CConst.EnumSound.ALERT_BUZZ, CConst.CNS_MSG_007, Color.White)
                    task.ProcessText = ""
                    task.ActionCd = "CANCEL_LABEL"
                    Return DoBaseExecute(task)
            End Select

        Catch ex As Exception

        End Try

        task.ProcessText = ""
        task.ActionCd = "LABEL"
        nextState = GetNextState(task)
        Return nextState
    End Function

    ''' <summary>
    ''' 終了メソッド(未使用)
    ''' </summary>
    ''' <param name="task"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overrides Function DoExit(ByVal task As CTaskBase) As Boolean
        Debug.WriteLine("CCANCELLabelState.DoExit")
        Return True
    End Function


End Class
